package com.hidao.platform.zike.web.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.hidao.basic.common.bean.Page;
import com.hidao.basic.common.vo.ResultMap;
import com.hidao.platform.ssh.common.vo.CommonSearchVo;
import com.hidao.platform.ssh.common.vo.OperVo;
import com.hidao.platform.ssh.common.vo.PageVo;
import com.hidao.platform.ssh.common.vo.StatusVo;
import com.hidao.platform.ssh.constant.CommonStatus;
import com.hidao.platform.ssh.web.basic.BaseController;
import com.hidao.platform.zike.entity.*;
import com.hidao.platform.zike.service.*;
import com.hidao.platform.zike.api.vo.CustomerVo;


@Controller
@RequestMapping(value = "/customer")
public class CustomerController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(CustomerController.class);
	@Autowired
	private CustomerService customerService;

	/** 查询列表 */
	@RequestMapping(value = { "/list", "/search" })
	public String list(Model model, CustomerVo customer, HttpServletRequest request) {
		if (request.getRequestURI().contains("/search")) {
			model.addAttribute("customer", customer);
			// 查询条件
			return "page/customer/search";
		}
		return "page/customer/list";
	}

	@RequestMapping(value = { "/itemView" })
	@ResponseBody
	public PageVo<CustomerVo> itemView(@RequestBody CommonSearchVo<CustomerVo> search) {
		PageVo<CustomerVo> page = new PageVo<CustomerVo>();
		CustomerVo query = search.getQueryParams(CustomerVo.class);
		try {
			Page<Customer> pageVo = this.customerService.findByPage(query, search.getPageable());
			if (CollectionUtils.isNotEmpty(pageVo.getContent())) {
				CustomerVo vo = null;
				for (Customer o : pageVo.getContent()) {
					vo = new CustomerVo();
					BeanUtils.copyProperties(o, vo);
					vo.setState(new StatusVo(CommonStatus.valueOf(o.getStatus(), null), null));
					vo.setOper(new OperVo(o.getId(), o.getStatus(), null, 0,1,2));
					page.putColumn(vo);
				}
			}
			page.setDraw(search.getDraw());
			page.setRecordsFiltered(pageVo.getTotalElements());
			page.setRecordsTotal(pageVo.getTotalElements());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}

	@RequestMapping(value = { "/show", "/new" })
	public String showOne(Integer id, Model model, HttpServletRequest request) {
		if (!StringUtils.isEmpty(id)) {
			Customer customer = this.customerService.getById(id);
			CustomerVo customerVo=new CustomerVo();
			BeanUtils.copyProperties(customer,customerVo);
			model.addAttribute("customer", customerVo);
		}
		return "page/customer/iframenew";
	}

	@RequestMapping(value = { "/save", "/edit" }, method = RequestMethod.POST)
	@ResponseBody
	public ResultMap saveOrEdit(@RequestBody CustomerVo vo, HttpServletRequest request) {
		ResultMap results = new ResultMap();
        try {
            this.customerService.saveOrUpdate(vo);
        } catch (Exception e) {
            results.putError(e.getMessage());
            logger.error("保存失败，{}", JSONObject.toJSONString(vo), e);
        }
		return results;
	}

	@RequestMapping(value = { "/delete" }, method = RequestMethod.POST)
	@ResponseBody
	public ResultMap delete(CustomerVo vo) {
		ResultMap results = new ResultMap();
		try {
			this.customerService.deleteById(vo.getId());
		} catch (Exception e) {
			results.putError(e.getMessage());
			logger.error("删除失败，{}", JSONObject.toJSONString(vo), e);
		}
		return results;
	}

	@RequestMapping("/updateStatus")
	@ResponseBody
	public ResultMap updateStatus(CustomerVo vo) {
		ResultMap results = new ResultMap();
		try {
			this.customerService.updateStatus(vo.getId());
		} catch (Exception e) {
			results.putError(e.getMessage());
			logger.error("状态更新失败，{}", JSONObject.toJSONString(vo), e);
		}
		return results;
	}

	@RequestMapping(value = { "/interchange" }, method = RequestMethod.POST)
	@ResponseBody
	public ResultMap interchange(Integer up, Integer down) {
		ResultMap results = new ResultMap();
		try {
			this.customerService.interchange(up, down);
		} catch (Exception e) {
			results.putError(e.getMessage());
			logger.error("排序失败，up{},down{}", up,down, e);
		}
		return results;
	}
}
